package cn.soulic.tools.redis.migration.handler;

import cn.soulic.tools.redis.migration.config.RedisMigrationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * List数据处理
 *
 * @author Soulic Yang
 * @since 1.0.0
 */
@Component(value = "list")
public class ListHandler extends AbstractHandler {
    private final Logger logger = LoggerFactory.getLogger(ListHandler.class);

    @Resource
    private RedisMigrationProperties properties;

    @Override
    public void doHandle(String key) {
        Long size = sourceCommands.llen(key);
        logger.debug("[{}]  size: {}", key, size);
        if (size == null || size <= 0) {
            return;
        }

        long start = 0;
        long stop = 0;
        while (stop <= size) {
            stop = start + properties.getMaxCount() - 1;
            logger.debug("start: {}  stop: {}", start, stop);
            List<String> listValue = sourceCommands.lrange(key, start, stop);
            targetCommands.rpush(key, listValue.toArray(new String[0]));
            start = stop + 1;
        }
    }
}
